/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmArticoli.java
 *
 * Created on 2-ago-2011, 14.40.24
 */
package UI;

import UI.articoli.dialogs.dlgArticoliSearch;
import UI.articoli.dialogs.dlgEditComponenti;
import classes.Articoli_Componenti;
import classes.Articolo;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mrossetti
 */
public class frmArticoli extends javax.swing.JFrame {

    /** Creates new form frmArticoli */
    Articolo Art;
    ArrayList<Articoli_Componenti> Componenti;
    public frmArticoli() {
        initComponents();
        BufferedImage img = null;
        try{
            img = ImageIO.read(this.getClass().getResourceAsStream("/resources/articoli/gear.png"));
        }catch(Exception e){
            e.printStackTrace();
        }
        setIconImage(img);
        setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width)/2, (Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height)/2);
        Art = new Articolo();
        BoundArticolo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        P = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtIDArticolo = new javax.swing.JTextField();
        cmdSearch = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtCodice = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        SP = new javax.swing.JScrollPane();
        TabComp = new javax.swing.JTable();
        cmdAddComponenti = new javax.swing.JButton();
        cmdDelComponenti = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        SP2 = new javax.swing.JScrollPane();
        txtNote = new javax.swing.JTextArea();
        cmdNuovo = new javax.swing.JButton();
        cmdSalva = new javax.swing.JButton();
        cmdCancella = new javax.swing.JButton();
        cmdEsci = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Anagrafica Articoli");

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("ID Articolo");

        txtIDArticolo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtIDArticoloFocusLost(evt);
            }
        });

        cmdSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/search.png"))); // NOI18N
        cmdSearch.setToolTipText("Ricerca articolo");
        cmdSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSearchActionPerformed(evt);
            }
        });

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Codice");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Nome");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Componenti");

        TabComp.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID Componente", "Codice", "Nome", "Quantità"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TabComp.getTableHeader().setReorderingAllowed(false);
        SP.setViewportView(TabComp);

        cmdAddComponenti.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/add.png"))); // NOI18N
        cmdAddComponenti.setToolTipText("Aggiungi telefono");
        cmdAddComponenti.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAddComponentiActionPerformed(evt);
            }
        });

        cmdDelComponenti.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/remove.png"))); // NOI18N
        cmdDelComponenti.setToolTipText("Aggiungi telefono");
        cmdDelComponenti.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdDelComponentiActionPerformed(evt);
            }
        });

        jLabel5.setText("Note");

        txtNote.setColumns(20);
        txtNote.setRows(5);
        SP2.setViewportView(txtNote);

        cmdNuovo.setText("Nuovo");
        cmdNuovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuovoActionPerformed(evt);
            }
        });

        cmdSalva.setText("Salva");
        cmdSalva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSalvaActionPerformed(evt);
            }
        });

        cmdCancella.setText("Cancella");
        cmdCancella.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancellaActionPerformed(evt);
            }
        });

        cmdEsci.setText("Esci");
        cmdEsci.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEsciActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PLayout = new javax.swing.GroupLayout(P);
        P.setLayout(PLayout);
        PLayout.setHorizontalGroup(
            PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PLayout.createSequentialGroup()
                        .addComponent(cmdNuovo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdSalva)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdCancella)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 271, Short.MAX_VALUE)
                        .addComponent(cmdEsci))
                    .addGroup(PLayout.createSequentialGroup()
                        .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(PLayout.createSequentialGroup()
                                .addComponent(txtIDArticolo, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtCodice, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
                            .addComponent(txtNome, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE)
                            .addGroup(PLayout.createSequentialGroup()
                                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(SP2)
                                    .addComponent(SP, javax.swing.GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmdDelComponenti, javax.swing.GroupLayout.PREFERRED_SIZE, 22, Short.MAX_VALUE)
                                    .addComponent(cmdAddComponenti, javax.swing.GroupLayout.PREFERRED_SIZE, 22, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        PLayout.setVerticalGroup(
            PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtIDArticolo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCodice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(PLayout.createSequentialGroup()
                        .addComponent(cmdAddComponenti)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdDelComponenti))
                    .addComponent(SP, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(SP2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmdNuovo)
                        .addComponent(cmdSalva)
                        .addComponent(cmdCancella))
                    .addComponent(cmdEsci))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        getContentPane().add(P, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void cmdAddComponentiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAddComponentiActionPerformed
        ((DefaultTableModel)TabComp.getModel()).addRow(new Object[TabComp.getColumnCount()]);
        TabComp.getSelectionModel().setSelectionInterval(TabComp.getRowCount()-1, TabComp.getRowCount()-1);
        TabComp.setValueAt("", TabComp.getSelectedRow(), 0);
        TabComp.setValueAt("", TabComp.getSelectedRow(), 1);
        TabComp.setValueAt("", TabComp.getSelectedRow(), 2);
        TabComp.setValueAt("", TabComp.getSelectedRow(), 3);
        dlgEditComponenti Dlg = new dlgEditComponenti(this, TabComp, true);
        Dlg.setVisible(true);
        if(TabComp.getValueAt(TabComp.getRowCount()-1, 0) != null){
            if(TabComp.getValueAt(TabComp.getRowCount()-1, 0).equals("")){
                ((DefaultTableModel)TabComp.getModel()).removeRow(TabComp.getRowCount()-1);
            }
        }else{
            ((DefaultTableModel)TabComp.getModel()).removeRow(TabComp.getRowCount()-1);
        }
}//GEN-LAST:event_cmdAddComponentiActionPerformed

    private void cmdDelComponentiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDelComponentiActionPerformed
        if(TabComp.getSelectedRow() >= 0){
            if(JOptionPane.showConfirmDialog(this, "Cancellare il componente selezionato?", "The WareHouse", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
                ((DefaultTableModel)TabComp.getModel()).removeRow(TabComp.getSelectedRow());
            }
        }
    }//GEN-LAST:event_cmdDelComponentiActionPerformed

    private void cmdNuovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuovoActionPerformed
        Art = new Articolo();
        BoundArticolo();
}//GEN-LAST:event_cmdNuovoActionPerformed

    private void cmdSalvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSalvaActionPerformed
        try{
            if(ControllaForm()){
                if(!txtIDArticolo.getText().equals(""))Art.setIDArticolo(Integer.parseInt(txtIDArticolo.getText()));
                Art.setCodiceArticolo(txtCodice.getText());
                Art.setNomeArticolo(txtNome.getText());
                Art.setNote(txtNote.getText());
                if(Art.save()){
                    JOptionPane.showMessageDialog(this, "Salvataggio Effettuato", "The WareHouse", JOptionPane.INFORMATION_MESSAGE);
                    Art.load(txtCodice.getText());
                    BoundArticolo();
                }else{
                    JOptionPane.showMessageDialog(this, "Salvataggio non Effettuato","The WareHouse", JOptionPane.ERROR_MESSAGE);
                }
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.getMessage(),"The WareHouse", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_cmdSalvaActionPerformed

    private void cmdCancellaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancellaActionPerformed
        if(Art.getIDArticolo() >= 0){
            if(JOptionPane.showConfirmDialog(this, "Si desidera cancellare l'articolo selezionato?","The WareHouse", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
                if(Art.delete()){
                    JOptionPane.showMessageDialog(this, "Articolo cancellato", "The WareHouse", JOptionPane.INFORMATION_MESSAGE);
                    Art = new Articolo();
                    BoundArticolo();
                }else{
                    JOptionPane.showMessageDialog(this, "Cancellazione annullata a causa di un errore sul database", "The WareHouse", JOptionPane.WARNING_MESSAGE);
                }
            }
        }
}//GEN-LAST:event_cmdCancellaActionPerformed

    private void cmdEsciActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEsciActionPerformed
        setVisible(false);
        dispose();
}//GEN-LAST:event_cmdEsciActionPerformed

    private void txtIDArticoloFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtIDArticoloFocusLost
        if(!txtIDArticolo.getText().trim().equals("")){
            try{
                Art.setIDArticolo(Integer.parseInt(txtIDArticolo.getText().trim()));
                Art.load();
                BoundArticolo();
            }catch(Exception e){
                JOptionPane.showMessageDialog(this, e.getMessage());
            }
        }
    }//GEN-LAST:event_txtIDArticoloFocusLost

    private void cmdSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSearchActionPerformed
        dlgArticoliSearch Search = new dlgArticoliSearch(this, true);
        Search.setVisible(true);
        Art.load();
        BoundArticolo();
    }//GEN-LAST:event_cmdSearchActionPerformed

    public void setArticolo(Articolo A){
        Art  = A;
    }
    
    public Articolo getArticolo(){
        return Art;
    }
    
    public void BoundArticolo(){
        if(Art.getIDArticolo()>=0){
            txtIDArticolo.setText(""+Art.getIDArticolo());
        }else{
            txtIDArticolo.setText("");
        }
        txtCodice.setText(Art.getCodiceArticolo());
        txtNome.setText(Art.getNomeArticolo());
        txtNote.setText(Art.getNote());
        BoundComponenti();
        
    }
    
    public void BoundComponenti(){
        Componenti = Art.getComponenti();
        ((DefaultTableModel)TabComp.getModel()).setRowCount(0);
        for(int i = 0;i<Componenti.size();i++){
            ((DefaultTableModel)TabComp.getModel()).addRow(new Object[TabComp.getColumnCount()]);
            TabComp.setValueAt(Componenti.get(i).getIDComponente(), i, 0);
            TabComp.setValueAt(Componenti.get(i).getCodice_componente(), i, 1);
            TabComp.setValueAt(Componenti.get(i).getNomeComponente(), i, 2);
            TabComp.setValueAt(Componenti.get(i).getQta(), i, 3);
        }
    }
    
    private boolean ControllaForm(){
        if(txtCodice.getText().equals("")){
           JOptionPane.showMessageDialog(this, "Inserire un codice articolo", "The WareHouse", JOptionPane.WARNING_MESSAGE);
           return false;
        }
        
        
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel P;
    private javax.swing.JScrollPane SP;
    private javax.swing.JScrollPane SP2;
    private javax.swing.JTable TabComp;
    private javax.swing.JButton cmdAddComponenti;
    private javax.swing.JButton cmdCancella;
    private javax.swing.JButton cmdDelComponenti;
    private javax.swing.JButton cmdEsci;
    private javax.swing.JButton cmdNuovo;
    private javax.swing.JButton cmdSalva;
    private javax.swing.JButton cmdSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField txtCodice;
    private javax.swing.JTextField txtIDArticolo;
    private javax.swing.JTextField txtNome;
    private javax.swing.JTextArea txtNote;
    // End of variables declaration//GEN-END:variables
}
